var express = require('express');
var jwt = require('jsonwebtoken')
var router = express.Router();
var { landlordModel } = require('../model/model');
var multiparty = require('multiparty');

function getCode() {
  let str = '';
  for (let i = 0; i < 6; i++) {
    str += Math.floor(Math.random() * 10);
  }
  return str;
}

const SMSClient = require('@alicloud/sms-sdk');


router.post('/get_sms', (req, res) => {
  // 15614231665
  let phoneNum = req.body.phoneNum
  console.log('手机号码', req.body.phoneNum)
  res.send({
    code: 200,
    msg: '发送成功',
    phoneNum,
    sms: getCode(),
    time: Date.now()
  })
  // let smsClient = new SMSClient({
  //   accessKeyId: 'LTAI5tMHZvdVEPLoDEkYDhyw', //配置阿里云的key
  //   secretAccessKey: 'SETLRi9Fkm6JNMwU72AFrIW8Jti0Kx' //
  // })
  // console.log(smsClient, '-----')
  // let str = getCode()

  // // 发送短信
  // smsClient.sendSMS({
  //   PhoneNumbers: phoneNum, //手机号
  //   SignName: '向手机发送信息', //配置阿里云的签名
  //   TemplateCode: 'SMS_467765039', //模板号
  //   TemplateParam: `{"code":${str}}` //模板参数
  // }).then((result) => {
  //   let { Code } = result
  //   if (Code === 'OK') {
  //     res.send({
  //       code: 200,
  //       msg: '发送成功',
  //       sms: str,
  //       time: Date.now()
  //     })
  //   }
  // }).catch((err) => {
  //   console.log(123456)
  //   console.log(err)
  //   res.send({
  //     code: 202,
  //     msg: '发送失败'
  //   })
  // })
  
})

// 登录接口
router.post('/lxj_login',async (req, res) => {
  console.log(req.body)
  let data=await landlordModel.findOne({phone:req.body.phoneNum})
  const accessToken = 'Bearer ' + jwt.sign(req.body, 'secret', { expiresIn: '60' })
  const refreshToken = 'Bearer ' + jwt.sign(req.body, 'secret', { expiresIn: '7D' })
  console.log(data)
  res.send({
    accessToken,
    refreshToken,
    data,
  })
})

// 注册接口
router.post('/lxj_register', async(req, res) => {
  let {phone}=req.body
  console.log(req.body)
  let data=await landlordModel.findOne({phone})
  console.log(data)
  if(!data){
    landlordModel.create(req.body)
    console.log(1)
  }
  res.send({
    code: 200,
    msg: '注册成功'
  })
})

// token刷新接口
router.post('/refresh', (req, res) => {
  // 刷新token
  const refreshToken = req.body.refreshToken
  const token = refreshToken.split(' ')[1]
  console.log(token)
  jwt.verify(token, 'secret', (err, data) => {
    if (err) {
      console.log(err)
      res.send({
        code: 401,
        msg: 'token过期,请重新登录'
      })
    } else {
      const accessToken = 'Bearer ' + jwt.sign({ name: data.name }, 'secret', { expiresIn: '5s' })
      const refreshToken = 'Bearer ' + jwt.sign({ name: data.name }, 'secret', { expiresIn: '20s' })
      res.send({
        accessToken,
        refreshToken,
        code: 200,
      })
    }
  })
})

// 上传图片
router.post('/upload', (req, res) => {
  let form = new multiparty.Form()
  form.uploadDir = 'upload'
  form.parse(req, (err, file, imgData) => {
    if (err) {
      console.log(err)
      return err
    }
    res.send({
      code: 200,
      path: 'http://127.0.0.1:3000/' + imgData.file[0].path
    })
  })
})


module.exports = router;
